package jaux.tank.api.controller.system;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;
import jaux.tank.api.controller.BaseController;
import jaux.tank.bean.annotion.core.BussinessLog;
import jaux.tank.bean.constant.Const;
import jaux.tank.bean.dictmap.RoleDict;
import jaux.tank.bean.dto.RoleDto;
import jaux.tank.bean.enumeration.BizExceptionEnum;
import jaux.tank.bean.exception.TankException;
import jaux.tank.bean.vo.front.Ret;
import jaux.tank.bean.vo.front.Rets;
import jaux.tank.dao.system.RoleRepository;
import jaux.tank.service.system.LogObjectHolder;
import jaux.tank.service.system.RoleService;
import jaux.tank.service.system.impl.ConstantFactory;
import jaux.tank.utils.BeanUtil;
import jaux.tank.utils.ToolUtil;
import jaux.tank.warpper.RoleWarpper;

/**
 * Role Controller.
 */
@RestController("roleApiController")
@RequestMapping("/api/role")
public class RoleController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private RoleService roleService;
    @ApiOperation(value = "查询角色列表")
    @GetMapping("/list")
    public Ret<Object> list(@RequestParam(required = false) String name,@RequestParam(required = false) String deptId){
        logger.info("role:{}",name);
        List roles = null;
        if(StringUtils.isNotBlank(name) && deptId!=null) {
        	roles = roleRepository.findByNameAndDeptid(name, deptId);
        }else if(StringUtils.isNotBlank(name)){
            roles = roleRepository.findByName(name);
        }else if(deptId!=null){
        	roles = roleRepository.findByDeptid(deptId);
        }else {
        	roles = (List) roleRepository.findAll();
        	
        }
        return Rets.success(new RoleWarpper(BeanUtil.objectsToMaps(roles)).warp());
    }

    @ApiOperation(value = "编辑角色")
    @PostMapping
//    @BussinessLog(value = "编辑角色", key = "name", dict = RoleDict.class)
    public Ret<Object> save(@RequestBody RoleDto role, BindingResult result){
        if (result.hasErrors()) {
            throw new TankException(BizExceptionEnum.REQUEST_NULL);
        }
        roleRepository.save(role);
        return Rets.success();
    }
    
    @ApiOperation(value = "删除角色")
    @DeleteMapping
    @BussinessLog(value = "删除角色", key = "roleId", dict = RoleDict.class)
    public Ret<Object> remove(String roleId){
        logger.info("id:{}",roleId);
        if (ToolUtil.isEmpty(roleId)) {
            throw new TankException(BizExceptionEnum.REQUEST_NULL);
        }

        //不能删除超级管理员角色
        if(roleId.equals(Const.ADMIN_ROLE_ID)){
            throw new TankException(BizExceptionEnum.CANT_DELETE_ADMIN);
        }
        //缓存被删除的角色名称
        LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(roleId));
        this.roleService.delRoleById(roleId);
        return Rets.success();
    }

    @ApiOperation(value = "配置角色权限")
    @PostMapping("/savePermisson")
    @BussinessLog(value = "配置角色权限", key = "roleId", dict = RoleDict.class)
    public Ret<Object> setAuthority(String roleId, String
            permissions) {
        if (ToolUtil.isOneEmpty(roleId)) {
            throw new TankException(BizExceptionEnum.REQUEST_NULL);
        }
        roleService.setAuthority(roleId, permissions);
        return Rets.success();
    }
}
